package com.dykj.fireFighting.server.socialUnit.xfxg.service.impl;

import com.dykj.fireFighting.api.socialUnit.xfxg.dto.FireGroupSaveDto;
import com.dykj.fireFighting.api.socialUnit.xfxg.entity.FireGroup;
import com.dykj.fireFighting.api.socialUnit.xfxg.entity.FireGroupDevice;
import com.dykj.fireFighting.api.socialUnit.xfxg.service.IFireGroupDeviceService;
import com.dykj.fireFighting.api.socialUnit.xfxg.service.IFireGroupService;
import com.dykj.fireFighting.common.core.service.impl.BaseServiceImpl;
import com.dykj.fireFighting.common.core.utils.UUIDUtils;
import com.dykj.fireFighting.server.socialUnit.xfxg.mapper.FireGroupMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;


import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 消防巡更-消防群组
 *
 * @Author JacksonTu
 * @Date  2021-06-28 14:54:24
 */
@Slf4j
@DubboService
@Transactional(rollbackFor = Exception.class)
public class FireGroupServiceImpl extends BaseServiceImpl<FireGroupMapper, FireGroup> implements IFireGroupService {

    @Resource
    private  IFireGroupDeviceService fireGroupDeviceService;

    @Transactional
    @Override
    public Boolean insertFireGroup(FireGroupSaveDto fireGroupSaveDto){
        FireGroup fireGroup=new FireGroup();
        log.info("消防群组:{}",fireGroupSaveDto.toString());
        fireGroup.setCreateBy(fireGroupSaveDto.getCreateBy());
        fireGroup.setCreateTime(new Date());
        fireGroup.setUnitId(fireGroupSaveDto.getUnitId());
        fireGroup.setName(fireGroupSaveDto.getName());
        fireGroup.setRemark(fireGroupSaveDto.getRemark());
        fireGroup.setCycle(fireGroupSaveDto.getCycle());
        String fireGroupId=UUIDUtils.get32UUID();
        fireGroup.setId(fireGroupId);
        log.info("消防群组写入数据:{}",fireGroup.toString());
        save(fireGroup);
       for(int i =0 ;i< fireGroupSaveDto.getDevice().size();i++){
           FireGroupDevice fireGroupDevice =new FireGroupDevice();

           fireGroupDevice.setId(UUIDUtils.get32UUID());
           fireGroupDevice.setDeviceId(fireGroupSaveDto.getDevice().get(i));
           fireGroupDevice.setFireGroupId(fireGroupId);
           fireGroupDevice.setType(0);
           log.info("智能中间设备：{}",fireGroupDevice.toString());
           fireGroupDeviceService.addFireGroup(fireGroupDevice);
       }
        for(int j =0 ;j< fireGroupSaveDto.getDeviceTradition().size();j++){
            FireGroupDevice fireGroupDevice =new FireGroupDevice();
            fireGroupDevice.setId(UUIDUtils.get32UUID());
            fireGroupDevice.setDeviceId(fireGroupSaveDto.getDeviceTradition().get(j));
            fireGroupDevice.setFireGroupId(fireGroupId);
            fireGroupDevice.setType(1);
            log.info("传统中间设备：{}",fireGroupDevice.toString());
            fireGroupDeviceService.addFireGroup(fireGroupDevice);

        }
       return true;
   }

   @Transactional
   @Override
    public Boolean updateFireGroup(FireGroupSaveDto fireGroupSaveDto){
        FireGroup fireGroup=new FireGroup();
        fireGroup.setUpdateTime(new Date());
        fireGroup.setUpdateBy(fireGroupSaveDto.getUpdateBy());
       // fireGroup.setUnitId(fireGroupSaveDto.getUnitId());
        fireGroup.setName(fireGroupSaveDto.getName());
        fireGroup.setRemark(fireGroupSaveDto.getRemark());
        fireGroup.setCycle(fireGroupSaveDto.getCycle());
        fireGroup.setId(fireGroupSaveDto.getId());
        updateById(fireGroup);

       fireGroupDeviceService.delFireGroupId(fireGroupSaveDto.getId());

       for(int i =0 ;i< fireGroupSaveDto.getDevice().size();i++){
           FireGroupDevice fireGroupDevice =new FireGroupDevice();

           fireGroupDevice.setId(UUIDUtils.get32UUID());
           fireGroupDevice.setDeviceId(fireGroupSaveDto.getDevice().get(i));
           fireGroupDevice.setFireGroupId(fireGroupSaveDto.getId());
           fireGroupDevice.setType(0);
           log.info("智能中间设备：{}",fireGroupDevice.toString());
           fireGroupDeviceService.addFireGroup(fireGroupDevice);
       }
       for(int j =0 ;j< fireGroupSaveDto.getDeviceTradition().size();j++){
           FireGroupDevice fireGroupDevice =new FireGroupDevice();
           fireGroupDevice.setId(UUIDUtils.get32UUID());
           fireGroupDevice.setDeviceId(fireGroupSaveDto.getDeviceTradition().get(j));
           fireGroupDevice.setFireGroupId(fireGroupSaveDto.getId());
           fireGroupDevice.setType(1);
           log.info("传统中间设备：{}",fireGroupDevice.toString());
           fireGroupDeviceService.addFireGroup(fireGroupDevice);
       }
        return true;
    }




}
